<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------


/**
 * Guestbook_model
 *
 * Model used to retrive guestbook entries for a website
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Guestbook_model extends Model {
	function Guestbook_model( ) {
		parent::Model( );
		
		log_message( 'debug', 'conlabz CMS : Guestbook_model class loaded' );
	}

	/**
	 * Get Guestbooks
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getGuestbooks( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'guestbook.id, 
							guestbook.name,
							guestbook.description' 
						 );
		$this->db->from( "guestbook" );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('guestbook.name desc'); 
		return $this->db->get( );
	}
	
	function getPrivateKeyById( $id ){
		$this->db->select( 'guestbook.recaptcha_private_key' );
		$this->db->from( "guestbook" );
		$this->db->where( "id", $id );
		$guestbook = $this->db->get( );
		$row = $guestbook->result( );
		return $row[0]->recaptcha_private_key;
	}
	
	/**
	 * Get Entries
	 *
	 * @access public
	 * @param string $domain Domain of the using website
	 * @return string
	 */
	function getEntries( $id ) {
		$this->db->select( 'guestbook_entry.*' );
		$this->db->from( "guestbook_entry" );
		
		$whereString = "`guestbook_entry`.`guestbook_id` = '".$id."'";
		
		$this->db->where( $whereString );
		
		return $query = $this->db->get( ); 
	}
	
	/**
	 * Insert new Guestbook
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		$data['website_id'] = $this->session->userdata( 'website_id' );
		//insert the new entry
		$this->db->insert( 'guestbook', $data );
	}	
	
	/**
	 * Insert new Entry
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert_entry( $data = array( ) ) {
		//insert the new entry
		$this->db->insert( 'guestbook_entry', $data );
	}	
	
	/**
	 * Update Guestbook
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {
		//update entry
		$this->db->update( 'guestbook', $data, array( 'id' => $id ) );
	}	
	
	/**
	 * Update Entry
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update_entry( $data = array( ), $id ) {
		//update entry
		$this->db->update( 'guestbook_entry', $data, array( 'id' => $id ) );
	}
	

	/**
	 * Constructs an array of the given guestbooks to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildGuestbookDropdown( ) {
		$guestbooks = $this->getGuestbooks( );
		
		$dropdown = array( );
		foreach( $guestbooks->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->name;
		}
		
		return $dropdown;
	}
	
	/**
	 * Get Entries for view file on the frontend
	 * 
	 * @param $blog_id
	 * @param $where
	 * @param $limit
	 * @return object
	 */
	function getEntriesView( $guestbook_id, $entry_id = NULL ) {
		//get name of the guestbook
		$this->db->select( 'guestbook.name, guestbook.recaptcha_public_key, guestbook.recaptcha_private_key, guestbook.description' );
		$this->db->from( 'guestbook' );
		$this->db->where( 'guestbook.id', $guestbook_id );
		$guestbook = $this->db->get( );
		$name = $guestbook->result_array( );
		$return = array( );
		if( !empty( $name ) ){
			$return['h1'] = $name[0]['name'];
			$return['recaptcha_public_key'] = $name[0]['recaptcha_public_key'];
			$return['recaptcha_private_key'] = $name[0]['recaptcha_private_key'];
			$return['description'] = $name[0]['description'];
		}
		//get entries
		$this->db->select( 'guestbook_entry.body,
							guestbook_entry.title,
							guestbook_entry.id,
							guestbook_entry.name,
							guestbook_entry.timestamp'
						 );
		$this->db->from( "guestbook_entry" );
		$this->db->where( "guestbook_entry.guestbook_id", $guestbook_id );
		if( !empty( $entry_id ) ){
			$this->db->where( "guestbook_entry.id", $entry_id );
		}
		$this->db->where( "guestbook_entry.active", '1' );
		$this->db->order_by( 'guestbook_entry.timestamp desc' );
		 
		$entries = $this->db->get( );
		foreach( $entries->result_array( ) as $row ){
			$return['entry'][$row['id']] = array( 'id' => $row['id'],
												  'title' => $row['title'],
												  'body' => $row['body'],
												  'name' => $row['name'],
												  'timestamp' => $row['timestamp']
												);
		}
		return $return;
	}
}
/* End of file guestbook_model.php */
/* Location: ./modules/guestbook/models/guestbook_model.php */